/*
 * LogBucket.java    0.0.1   22/ott/2009
 *
 * Copyright (c) 2009 mentalsmash.org
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * The use of the Apache License does not indicate that this project is
 * affiliated with the Apache Software Foundation.
 */
package org.mentalsmash.tazio.transactions.control.impl;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

import org.mentalsmash.tazio.transactions.control.TazioTransaction;
import org.mentalsmash.tazio.transactions.model.IllegalSeqNoException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.collect.Sets;

/**
 * Utility container for maintaining a bunch of active transactions having the
 * same sequence number
 * 
 * @version 0.0.1 22/ott/2009
 * @author Andrea Reale <andrea.reale@gmail.com>
 * 
 */
class LogBucket {
    private final static int INITIAL_CAPACITY = 10;
    private final static Logger log = LoggerFactory.getLogger(LogBucket.class);

    private final long seqNo;
    private final HashSet<TazioTransaction> bucket = Sets
	    .newHashSetWithExpectedSize(INITIAL_CAPACITY);

    private int bucketSize = 0;

    /**
     * Create a new LogBucket for the given sequence number
     * 
     * @param seqNo
     *            the sequence number of the created log bucket
     */
    LogBucket(final long seqNo) {
	// FIXME: make sure this has to be commented
	if (seqNo < 0) {
	    log
		    .error("Tried to create a LogBucket with negative sequence number");
	    throw new IllegalSeqNoException(
		    "Sequence Numbers must be non negative");
	}
	this.seqNo = seqNo;
    }

    /**
     * Create a new LogBucket for the given sequence number and add to it the
     * given transaction
     * 
     * @param seqNo
     *            the sequence number of the created log bucket
     * @param tx
     *            the transaction to initially add to the log bucket
     */
    public LogBucket(final long seqNo, final TazioTransactionImpl tx) {
	this(seqNo);
	if (tx.getSeqNo() != this.getSeqNo()) {
	    log
		    .error(
			    "Transaction having sequence number ? was not added to bucket having seqNo ?",
			    tx.getSeqNo(), this.getSeqNo());
	    throw new IllegalSeqNoException(
		    "Cannot add transaction to bucket: seqNo does not match");
	}
	this.bucket.add(tx);
    }

    /**
     * Adds transaction <code>tx</code> to the bucket if not already present
     * 
     * @param tx
     *            the transaction instance to add to the bucket
     * @return <code>true</code> if <code>tx</code> was not present in the
     *         bucket and therefore was added. Otherwise returns <code>
     *         false</code>
     */
    public synchronized boolean addToBucket(final TazioTransaction tx) {
	final boolean added = this.bucket.add(tx);
	if (added) {
	    this.bucketSize++;
	}
	return added;
    }

    /**
     * Removes transaction <code>tx</code> from the bucket if present. <br />
     * Otherwise does nothing
     * 
     * @param tx
     *            the transaction instance to remove from the bucket
     * @return <code>true</code> if <code>tx</code> was removed from the bucket.
     *         Otherwise <code> false</code>
     */
    public synchronized boolean removeFromBucket(final TazioTransaction tx) {
	final boolean removed = this.bucket.remove(tx);
	if (removed) {
	    this.bucketSize--;
	}
	return removed;
    }

    /**
     * Returns an unmodifiable set of {@link TazioTransactionImpl}s contained in
     * this bucket
     * 
     * @return an unmodifiable set of {@link TazioTransactionImpl}s contained in
     *         this bucket
     */
    public synchronized Set<TazioTransaction> getTransactions() {
	return Collections.unmodifiableSet(this.bucket);
    }

    /**
     * Returns the sequence number associated to this bucket.
     * <p>
     * It is the sequence number of every transaction in this bucket
     * </p>
     * 
     * @return
     */
    public long getSeqNo() {
	return this.seqNo;
    }

    public synchronized int getBucketSize() {
	return this.bucketSize;
    }

    public synchronized boolean isEmpty() {
	return this.bucketSize == 0;
    }
}
